package com.vcredit.creditcat.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vcredit.creditcat.framework.util.MD5Encoder;
import com.vcredit.creditcat.model.Role;
import com.vcredit.creditcat.model.UserRole;
import com.vcredit.creditcat.security.SecurityHelper;
import com.vcredit.creditcat.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.User;
import com.vcredit.creditcat.service.UserService;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@RequestMapping("/list")
	public ModelAndView  showPageUser(HttpServletRequest request) {
		return new ModelAndView("/user/userList");
	}
	@ResponseBody
	@RequestMapping("/listUser")
	public Page<User> findByPage(HttpServletRequest request) {
		return userService.findByPage(makeDefaultCriteria(request));
	}

	@ResponseBody
	@RequestMapping("/showAllRoles")
	public List<Role> findAllRoles() {
		List<Role> userRoles=roleService.findByUserId(SecurityHelper.getUserId());
		boolean flag=false;
		for(Role userRole:userRoles){
			if("channel".equals(userRole.getRoleName())){
				flag=true;
				break;
			}
		}
		List<Role> roles=roleService.findAll();
		if(flag){
			roles=roleService.findChannelRoles();
		}
		return roles;
	}

	@ResponseBody
	@RequestMapping("/addUser")
	@Transactional
	public String add(@RequestBody User user, HttpServletRequest request) {
		user.setUserPwd(MD5Encoder.encode(user.getUserPwd()));
		userService.insert(user);
		Long userId=user.getUserId();
		String roleIds=user.getRoleIds();
		String[] roleIdArray=roleIds.split(",");
		for(String roleId:roleIdArray){
			UserRole userRole=new UserRole();
			userRole.setUserId(userId);
			userRole.setRoleId(Long.valueOf(roleId));
			userService.insertUserRole(userRole);
		}
		return SUCCESS;
	}

	@RequestMapping("/updateShow")
	public ModelAndView updateShow(HttpServletRequest request,
			ModelAndView model) {
		String id = request.getParameter("id");
		User user = userService.findById(Long.parseLong(id));
		if (id == null || id.trim().length() == 0) {
			model.setViewName("/user/error");
		} else {
			model.addObject("id", id);
			model.addObject("name", user.getUsername());
			model.addObject("passWord", user.getUserPwd());
			model.addObject("status", user.getUserStatus());
			model.addObject("userName", user.getUsername());		
			model.setViewName("/user/edit");
		}
		return model;
	}
	
	@ResponseBody
	@RequestMapping("/updateDialog")
	public User updateDialog(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		return userService.findById(Long.parseLong(id));
	}

}
